package com.sunl.mall.ali.mq.autoconfigure.bean;


import com.sunl.mall.ali.mq.autoconfigure.MqEnvProperties;
import com.sunl.mall.ali.mq.consumer.RocketMqConsumerBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.util.Properties;

/**
 * 将用户配置设置到mq的properties中
 *
 * Constructor > @PostConstruct > InitializingBean > init-method
 */
@Slf4j
@EnableConfigurationProperties({MqEnvProperties.class})
public class RocketMqConsumerPostBean extends RocketMqConsumerBean implements InitializingBean {
    /**
     * 注入值的位置 {@link com.sunl.mall.ali.mq.autoconfigure.RocketMqConsumerBeanPostProcessor}
     */
    private String beanName;
    private final MqEnvProperties mqEnvProperties;

    public RocketMqConsumerPostBean(MqEnvProperties mqEnvProperties) {
        this.mqEnvProperties = mqEnvProperties;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (this.mqEnvProperties != null) {
            Properties properties = this.mqEnvProperties.getConsumers().get(this.getBeanName());
            if (properties == null || properties.isEmpty()) {
                throw new IllegalArgumentException("########### Can not found Consumer[" + this.beanName + "] config from properties. see MqEnvProperties.class");
            }
            log.info("########### found Consumer[" + this.beanName + "] config from properties. start to setProperties");
            super.setProperties(properties);
        }
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public String getBeanName() {
        return beanName;
    }

    public MqEnvProperties getMqEnvProperties() {
        return mqEnvProperties;
    }
}